// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    @property
    direction: number = 0 // 0 停着 1 上 2 下 3 左 4 右

    // LIFE-CYCLE CALLBACKS:

    lastDirection: number = 0 // 最后一次是什么方向 一般都是 3 4
    


    onLoad () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        this.node.on('keydown', this.onKeyDown,this)
    }

    start () {
        this.node.x += 200
        this.node.setScale(2.5, 2.5)
        this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onKeyDown,this)
    }

    onKeyUp(evt) {
        this.direction = 0
    }

    onKeyDown(evt) {
        switch(evt.keyCode) {
            case cc.macro.KEY.a:
                if (this.lastDirection == 4) {
                    this.node.setScale(-2.5, 2.5)
                    this.node.x -= 10
                }
                this.direction = 3
                this.lastDirection = 3
                break;
            case cc.macro.KEY.s:
                break;
            case cc.macro.KEY.d:
                if (this.lastDirection == 3) {
                    this.node.setScale(2.5, 2.5)
                    this.node.x += 10
                }
                this.direction = 4
                this.lastDirection = 4
                break;
            case cc.macro.KEY.w:
                break;
            case cc.macro.KEY.space:
                break;
        }
    }
    update (dt) {
        switch(this.direction) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                this.node.x -= 5
                break;
            case 4:
                this.node.x += 5
                break;
        }
    }
}
